/*
脚本说明:选中角色后，进入权限分配页面 ，对于扩展权限的保存
程序员:
*/

//脚本唯一关键字,通过该关键字找到对应脚本，每个脚本必须有该描述。
var v_script_id = 'authsetter_5';
//脚本状态,0:该脚本不许执行，1:该脚本可以执行
var v_status = '1';
//脚本逻辑写在run语句之间
if (run  && action.getAuthority().is(5,"9")){
	var user = action.getSession().getAttribute("user");
	var roleid = action.form.getInteger('roleid');
	var authids = action.form.getString('authids');
	var dynanodeset = action.form.getString('dynanodeset');
	var roleAuthKind = action.form.getString('roleAuthKind');
	//println('roleid='+roleid);
	//println('authids='+authids);
	//println('dynanodeset='+dynanodeset);
	//println('roleAuthKind='+roleAuthKind);
		
	var sql = '';
	var ret = '';
	action.retString = '添加成功!';
	//当前为扩展权限操作,h_auth_dyna_role_infos
	var _roleAuthKind = roleAuthKind.split(',');
	var _dynanodeset = dynanodeset.split(',');
	//返回 树节点标题
	sql = "select title from h_auth_trees where id =" + authids ;	
	var treetitle = action.dbUtil.getSampleStringValue(sql)
	//返回 map<所有权限种类ID，权限名称>
	sql = "select kind,name from h_auth_kinds ";	
	var allkindsmap = action.buildHashMap(sql,'kind','name');
	//返回authid下的所有扩展节点  map<扩展节点id， name>
	sql = 'select * from h_auth_dyna where authid=' + authids;
	var map = action.dbUtil.findFirst("", sql);
	var tablename = map.get('tname');
	var vcol = map.get('vcol');
	var pcol = map.get('pcol');
	var condition = map.get('condition');
	sql = ' select ' + vcol + ' as vcol ,' + pcol + ' as pcol from ' + tablename + ' ' + condition;
	var extentmap = action.buildHashMap(sql,'pcol','vcol');	 
		
	 
	for(i=0; i<_dynanodeset.length; i++){
		if(user.getRolekind()>=3){
			//普通用户:返回节点下可用的权限类型 map<权限种类ID，权限名称>
			sql = "select kind,kindname from v_auth_all where authid =" + authids + " and pcolvalue="+_dynanodeset[i] ;
			if (user.getRoleids('roleid','or') == null){
				sql += ' and userid = '+ user.getId() ;
			}else{
				sql += ' and (' + user.getRoleids('roleid','or') + ' or userid = '+ user.getId() +')';
			}
		}else{
			sql = "select kind,kindname from v_auth_id_kinds where authid =" + authids ;
		}
		var kindmap = action.buildHashMap(sql,'kind','kindname');
		for(j=0; j<_roleAuthKind.length; j++){
			if(kindmap.get(_roleAuthKind[j])==null){
				action.errorCode = 2; 
				ret += '<font color=red > 失败：无权添加 节点[' + 
							treetitle + '>>' + extentmap.get(_dynanodeset[i]) + '] '+ 
							allkindsmap.get(_roleAuthKind[j]) + ' 权限  !</font><br>';
				continue;
			}
			sql = "select count(id) from h_auth_dyna_role_infos where authid = " + 
						authids + " and pcolvalue = " + _dynanodeset[i] + " and authkind = " +
						_roleAuthKind[j] + " and roleid = " + roleid ;
			if( action.dbUtil.getSampleIntegerValue(sql) == 0 ){
				sql = "insert into h_auth_dyna_role_infos(id,authid,pcolvalue,authkind,roleid) " +
						" values(h_auths_dyna_role_info_seq.nextval,"+authids+","+_dynanodeset[i] +","+
						_roleAuthKind[j]+"," + roleid + ")";
				if(action.dbUtil.update("",sql)==0) 
				{
					action.errorCode = 2; 
					ret += '<font color=red > 失败：节点[' + treetitle + '>>' + 
								extentmap.get(_dynanodeset[i]) + ']添加 '+ 
								kindmap.get(_roleAuthKind[j]) + ' 权限 失败 !</font><br>';
				}  
			}else{
				action.errorCode = 2; 
				ret += '<font color=red > 已存在：节点[' + treetitle + '>>' + 
							extentmap.get(_dynanodeset[i]) + ']添加 '+ 
							kindmap.get(_roleAuthKind[j]) + ' 权限 失败 !</font><br>';	
			}
		}
	}
	
	if (action.errorCode ==2){
		action.retString = ret;
	}
	//刷新权限分配缓存
	//if (action.errorCode ==0){
		com.handy.plugin.auth.TAuthCache.getInstance().loadAuthDynaRoleInfos();
	//}
	
	 

}